package cn.com.dhcc.ddi.datasource.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.dbhandler.SingleTransationCircleWithOutResult;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datasource.dao.DataSourceDao;
import cn.com.dhcc.ddi.datasource.dao.DataSourceMysqlDao;
import cn.com.dhcc.ddi.datasource.dao.TableColDao;
import cn.com.dhcc.ddi.datasource.vo.DataSourceVo;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
/**
 * 
 * mysql数据源服务类
 * @Author: fxx
 * @date: 2017-5-16
 *
 */
@Service
public class DataSourceMysqlService extends BaseService {
	@Autowired
	DataSourceMysqlDao dao;
	@Autowired
	DataSourceDao datasourceDao;
	@Autowired
	TableInfoService tableInfoService;
	@Autowired
	TableColService tableColService;
	@Autowired
	TableColDao tableColDao;
	
    /**
     * 
     * @MethodName: [getUserTableForMysql]
     * @Description:[获取mysql表信息]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-16]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [List<TableInfoVo>]
     * @Throws:     [Exception]
     *
     */
	public List<TableInfoVo> getUserTableForMysql(DataSourceVo vo) throws ServiceException, NiceException{
		
		try {
			//根据数据源初始化dao
			dao.initDao(vo);
			final String schema = getSchemaForMysql();
			final List<TableInfoVo> list = new ArrayList<TableInfoVo>();
			final List<TableInfoVo> userTableList  = getUserTablesForMysql(schema);
			dao.getDao().doInSingleTransationCircle("元数据抽取", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						for (Iterator<TableInfoVo> iterator = userTableList.iterator(); iterator.hasNext();) {
							TableInfoVo userTable = iterator.next();
							String tableName = userTable.getTableName();
							List<TableColVo> cols = dao.getUserTabColumnsForMysql(tableName, schema);
							userTable.setTableColVos(cols);
							list.add(userTable);
						}
					} catch (DaoException e) {
						throw new RuntimeException("获取表元数据错误", e);
					}
				}
			});
			return list;
		} catch (DaoException e) {
			throw new ServiceException("获取表元数据错误", e);
		}
	}
	/**
	 * 抽取表结构，包括授权和自建的表和视图  Mysql
	 * @param vo
	 * @return
	 * @throws ServiceException
	 */
	public List<TableInfoVo> getUserTablesForMysql(String schema) throws ServiceException{
		List<TableInfoVo> userTableList = new ArrayList<TableInfoVo>();
		try {
			//抽取授权的表和视图
			userTableList.addAll( dao.getUserTablesForMysql(schema.toLowerCase()));
		} catch (DaoException e) {
			throw new ServiceException("获取表元数据错误", e);
		}
		return userTableList;
	}
	/**
	 * 抽取表结构，包括授权和自建的表和视图  Oracle
	 * @param vo
	 * @return
	 * @throws ServiceException
	 */
	public String getSchemaForMysql() throws ServiceException{
		String schema = "";
		try {
			 schema = dao.getSchemaForMysql();
		} catch (DaoException e) {
			throw new ServiceException("获取表元数据错误", e);
		}
		return schema;
	}
	
	/**
	 * 
	 * @Title: update
	 * @Description: 数据源信息修改
	 * @param @param vo
	 * @param @param tbInfoList
	 * @param @param tbColInfoList
	 * @param @throws DaoException 参数
	 * @return void 返回类型
	 * @throws
	 */
	public void update(final DataSourceVo vo, final List<TableInfoVo> tbInfoList, final List<TableColVo> tbColInfoList) throws DaoException, ServiceException {
		try {
			CM.getDao().doInSingleTransationCircle("修改数据源信息", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						datasourceDao.updateDataSource(vo);
						tableInfoService.updateTableInfo(tbInfoList);
						tableColService.updateOrInsertTableColInfo(tbInfoList, tbColInfoList);
					} catch (Exception e) {
						throw new RuntimeException("数据源信息修改失败", e);
					}
				}
			});
		} catch (DaoException e) {
			throw new DaoException("数据源信息修改失败", e);
		}
	}
	

}
